<?php

namespace AdminBundle\Controller;


use BaseBundle\Services\ArticleClassService;
use BaseBundle\Services\ArticleService;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ArticleController extends AdminController
{
    /**
     * @var ArticleService
     */
    private $articleService;

    /**
     * @var ArticleClassService
     */
    private $articleClassService;

    public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer($container);
        if (!empty($container)){
            $this->articleService = new ArticleService($container);
            $this->articleClassService = new ArticleClassService($container);
        }
    }

    /**
     *
     *
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function articlesAction()
    {
        $r = $this->inlet($this->getParameter('return_page'),true);
        if($r !== true){
            return $r;
        }
        $this->adminStrategy->setUrlMark('articles');

        $query['title_like'] = self::$request->query->get('title','');
        $query['created_at_start'] = self::$request->query->get('created_at_start','');
        $query['created_at_end'] = self::$request->query->get('created_at_end','');
        $query['is_recommend'] = self::$request->query->get('is_recommend','');

        $data = $this->articleService->getList(
            array_merge(
                ['join_article_class' => 1, 'select_article_class_msg' => 1 ],
                $query
            )
        );

        $data['is_true_arr'] = $this->getParameter('is_true_arr');
        return $this->render('AdminBundle:admin/Article:list.html.twig', $data);
    }

    /**
     *
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function editPageAction()
    {
        $r = $this->inlet($this->getParameter('return_page'),true);
        if($r !== true){
            return $r;
        }
        $this->adminStrategy->setMenuTag('articles');

        $id = self::$request->query->get('id','');

        if (!empty($id)){
            $data['info'] = $this->articleService->getInfo($id, [], 1);
        }
        $data['article_class_arr'] = $this->articleClassService->getAllListUseSelect()['info'];
        $data['is_true_arr'] = $this->getParameter('is_true_arr');

        return $this->render('AdminBundle:admin/Article:editPage.html.twig', $data);
    }

    /**
     *
     *
     * @return bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function insertOrUpdateAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if($r !== true){
            return $r;
        }
        $this->adminStrategy->setMenuTag('articles');

        $data = self::$request->request->all();

        $res = $this->articleService->insertOrUpdate($data['id'], $data);
        if ($res === false){
            return $this->response(self::$error->getError());
        }

        return $this->response($this->getParameter('success_action_msg'), 0);
    }

    /**
     *
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function deleteAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if($r !== true){
            return $r;
        }
        $this->adminStrategy->setMenuTag('articles');

        $id = self::$request->request->get('id','');

        if (empty($id)){
            return $this->response($this->getParameter('parameter_error_msg'));
        }
        $this->articleService->delete($id);

        return $this->response($this->getParameter('success_action_msg'), 0);
    }

    /**
     *
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function editIsRecommendAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), true);
        if($r !== true){
            return $r;
        }
        $this->adminStrategy->setMenuTag('articles');

        $article_id = self::$request->request->get('article_id','');
        $is_recommend = self::$request->request->get('is_recommend',1111);

        if (empty($article_id)){
            return $this->response($this->getParameter('parameter_error_msg'));
        }

        $this->articleService->updateOther(['is_recommend' => $is_recommend], ['id' => $article_id]);
        return $this->response($this->getParameter('success_action_msg'), 0);
    }
}
